/*
 * Copyright (C) 2008 by Xume.
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
package org.xume.critann;

import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

import org.xume.critann.annotations.Criteria;
import org.xume.critann.callback.AnnotationCallback;
import org.xume.critann.handler.AnnotationHandler;

/**
 * $Id: CriteriaBeanInspectionResults.java 20 2008-10-13 19:32:10Z johan.siebens $
 * 
 * @author Johan Siebens
 */
class CriteriaBeanInspectionResults {

	public static CriteriaBeanInspectionResults build(Class<?> criteriaBeanClass, List<AnnotationHandler> handlers) {
		Criteria criteriaAnnotation = criteriaBeanClass.getAnnotation(Criteria.class);

		if (criteriaAnnotation != null) {
			CriteriaBeanInspectionResults result = new CriteriaBeanInspectionResults(criteriaAnnotation.entityClass());

			try {
				Annotation[] classAnnotations = criteriaBeanClass.getAnnotations();
				for (Annotation annotation : classAnnotations) {
					for (AnnotationHandler handler : handlers) {
						if (handler.supports(annotation)) {
							result.add(handler.handleAnnotation(criteriaBeanClass, annotation));
						}
					}
				}

				PropertyDescriptor[] descriptors = Introspector.getBeanInfo(criteriaBeanClass).getPropertyDescriptors();
				for (PropertyDescriptor descriptor : descriptors) {
					Method method = descriptor.getReadMethod();
					if (method != null) {
						Annotation[] annotations = method.getAnnotations();
						for (Annotation annotation : annotations) {
							for (AnnotationHandler handler : handlers) {
								if (handler.supports(annotation)) {
									result.add(handler.handleAnnotation(criteriaBeanClass, annotation, method));
								}
							}
						}
					}
				}

				return result;
			} catch (InvalidCriteriaBeanConfigurationException e) {
				throw e;
			} catch (Exception e) {
				throw new InvalidCriteriaBeanConfigurationException(criteriaBeanClass, "Error parsing criteria bean", e);
			}
		} else {
			throw new InvalidCriteriaBeanConfigurationException(criteriaBeanClass, "Criteria class not annotated with @Criteria");
		}
	}

	private List<AnnotationCallback<?>> callbacks;

	private Class<?> entityClass;

	private CriteriaBeanInspectionResults(Class<?> entityClass) {
		this.entityClass = entityClass;
		this.callbacks = new ArrayList<AnnotationCallback<?>>();
	}

	public Class<?> getEntityClass() {
		return entityClass;
	}

	public List<AnnotationCallback<?>> getCallbacks() {
		return callbacks;
	}

	private void add(AnnotationCallback<?> callback) {
		if (callback != null) {
			this.callbacks.add(callback);
		}
	}

}
